// pages/mine/datum/index.js
const app = getApp();
var serverUrl = app.globalData.serverUrl;
var qiniuUrl = app.globalData.qiniuUrl;
const qiniuUploader = require("../../../utils/qiniuUploader");

// 初始化七牛相关参数
function initQiniu() {
  var options = {
    region: 'ECN', // 华东区
    uptokenURL: serverUrl+'api/v1/common/qiniu/gettoken',
    // uptoken: 'xxx',
    domain: qiniuUrl,
    shouldUseQiniuFileName: false
  };
  qiniuUploader.init(options);
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    headimg:"/asset/img/boy.png",
    userInfo:{},
    auth:false,
    address:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var userInfo = wx.getStorageSync("userInfo");
    if ( userInfo.uid ) {
      that.setData({
        userInfo:userInfo,
        auth:userInfo.is_auth
      })
      if ( userInfo.headimg ) {
        that.setData({
          headimg:userInfo.headimg
        })
      } else {
        if ( userInfo.sex == 1 ) {
          that.setData({
            headimg:'/asset/img/girl.png'
          })
        } else {
          that.setData({
            headimg:'/asset/img/boy.png'
          })
        }
      }
    } else {
      wx.redirectTo({
        url: '../login/index',
      })
    }
    let ids = '';
    if(userInfo.prov && userInfo.city && userInfo.area){
      ids = userInfo.prov+','+userInfo.city+','+userInfo.area;
      wx.request({
        url: serverUrl +'api/v1/common/map/getaddressname',
        method:"GET",
        data:{ids:ids},
        dataType:"json",
        success:function(data){
          data = data.data;
          if(data.code == 200){
            that.setData({
              address:data.data.name
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  edit:function(){
    wx.navigateTo({
      url: '../edit/index',
    })
  },

  /**头像编辑**/
  uploads:function(){
    var that = this;
    initQiniu();
    wx.chooseImage({
      count:1,
      success: function (res) { 
        console.log(res);
        if(res.tempFiles[0]['size'] >= 2048*1000){
          wx.showToast({
            title: '图片太大啦!请选择2M以内的图片哦',
            icon: 'none',
            duration: 1500
          });
          return false;
        } else {
          let filePath = res.tempFilePaths[0];
          qiniuUploader.upload(filePath, (result) => {
            let data = {};
            data.image = result.imageURL;
            data.uid = wx.getStorageSync("userInfo").uid;
            wx.request({
              url: serverUrl +'api/v1/users/upload/headimg',
              method:"POST",
              dataType:'json',
              data:data,
              success:function(data){
                data = data.data;
                if(data.code == 200){
                  let userInfo = wx.getStorageSync("userInfo");
                  userInfo.headimg = result.imageURL;
                  wx.setStorageSync("userInfo", userInfo);
                  that.setData({
                    headimg: result.imageURL
                  })
                } else {
                  wx.showToast({
                    title: '上传失败!【'+data.message+'】',
                    icon: "none",
                    duration: 2000
                  })  
                }
              },
              error:function(data){
                wx.showToast({
                  title: '服务器错误!头像上传失败',
                  icon:"none",
                  duration:2000
                })
              }
            })
          }, (error) => {
            wx.showToast({
              title: '上传失败!失败原因'+JSON.stringify(error),
              icon:"none",
              duration:2000
            })
          }, null, (progress) => {
            console.log('上传进度', progress.progress)
            console.log('已经上传的数据长度', progress.totalBytesSent)
            console.log('预期需要上传的数据总长度', progress.totalBytesExpectedToSend)
          })
        }
      },
    })
  }

})